import WxValidate from '../../../utils/WxValidate'
import {InviteModel} from "../../../models/invite";

const inviteModel = new InviteModel()
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        businessId: '',
        toUserId: '',
        from: '',
    },
    formSubmit(e) {
        let _this = this
        // 校验表单
        const params = e.detail.value
        if (!_this.WxValidate.checkForm(params)){
            const error = _this.WxValidate.errorList[0];
            _this._showModel(error.msg)
            return false
        }
        params.businessId = this.data.businessId
        params.toUserId = this.data.toUserId
        console.log(params)
        wx.showModal({
            title: '提示',
            content: '确定要发送约拍邀请',
            success (res) {
                if (res.confirm) {
                  _this.submitInfo(params) 
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    _showModel(msg) {
        wx.showToast({
            title: msg,
            icon: 'none'
        })
    },
    submitInfo(params) {
        let _this = this
        if (_this.data.from == 'works') {
            _this._addInviteForWorks(params)
        }else {
            _this._addInviteForShoot(params)
        }
        
    },
    _addInviteForWorks(params) {
        inviteModel.addWorksInviteInfo(params).then(res=>{
            wx.navigateBack({
                delta: 1,
                success: function () {
                  app.showInfo('发送邀请成功！', app.SUCCESS)
                }
            })

        })
    },
    _addInviteForShoot(params) {
        inviteModel.addShootInviteInfo(params).then(res=>{
          wx.navigateBack({
            delta: 1,
            success: function () {
              app.showInfo('发送邀请成功！', app.SUCCESS)
            }
          })
        })
    },
    initValidate() {

        const rules = {
            reason: {
                required: true,
                rangelength: [1, 250]
            },
            phone: {
                required: true,
                tel: true,
            },
            weChat: {
                rangelength: [1, 30]
            },
            weibo: {
                rangelength: [1, 100]
            },
        }
        const messages = {
            reason: {
                required: '请输入约拍理由',
                rangelength: '约拍理由为1~250个汉字',
            },
            phone: {
                required: '请输入11位手机号码',
                tel: '请输入正确的手机号码',
            },
            weChat: {
                rangelength: '微信号为1~30个字符'
            },
            weibo: {
                rangelength: '微博号为2~50个字符'
            }
        }
        this.WxValidate = new WxValidate(rules, messages)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            businessId: parseInt(options.businessId),
            toUserId: parseInt(options.userId),
            from: options.from,
        })
        this.initValidate()
        let isIphoneX = app.globalData.isIphoneX;
        this.setData({
            isIphoneX: isIphoneX
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
